package com.camps.sdk.encrypt;

import com.camps.sdk.common.ExceptionCode;
import com.camps.sdk.entity.WhiteCall;
import com.camps.sdk.exception.BaseException;
import com.camps.sdk.utils.UnicodeUtil;

/**
 * @Author ldaoliang
 * @Date create in 10:11 2021/9/1
 * @Description
 */
public class WhiteCallCommand implements BuildCommand {
    @Override
    public String splicingParameters(String imei,  Object params) {
        if(!(params instanceof WhiteCall)){
            throw new BaseException(ExceptionCode.PARAMETER_ERROR);
        }
        WhiteCall whiteCall = (WhiteCall)params;
        String commandNumber = whiteCall.getCommandNumber();
        String name = whiteCall.getName();
        String phone = whiteCall.getPhone();
        Integer type = whiteCall.getType();
        Integer pictureId = whiteCall.getPictureId();
        Integer sequence = whiteCall.getSequence();
        if(sequence == null || sequence > 25){
            throw new BaseException(ExceptionCode.WHITE_CALL_SEQUENCE_ERROR);
        }
        // 判断姓名是否为空，去决定是否进行编码
        if(name != null && ! "".equals(name)){
            name = UnicodeUtil.gbEncoding(name);
        }
        StringBuilder sb = new StringBuilder("*WT,");
        sb = sb.append(imei).append(",PBWL,").append(commandNumber).append(",").append(sequence).append(",")
                // 连续拼接两个逗号，是因为默认wl_imei为空
                .append(phone).append(",").append(type).append(",,").append(name).append(",").append(pictureId)
                .append("#");
        return sb.toString();
    }
}
